<?php

namespace App\Http\Controllers\Backend\Setting;

use App\Events\SettingLogEvent;
use App\Http\Requests\Backend\Setting\SettingRepayRateRequest;
use App\Models\SettingLog;
use App\Models\SettingRepayRate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SettingRepayRateController extends Controller
{
    use \App\Library\Traits\SettingLog;
    const SETTING_TABLE = "setting_repay_rate";
    public function __construct()
    {
        $this->middleware('auth:backend');
        $this->user = auth('backend')->user();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $rateInfo = SettingRepayRate::getRateList();
        return success($rateInfo);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SettingRepayRateRequest $request)
    {
        $rateInfo = $this->extractInputFromRules($request,$request->rules());
        $result = SettingRepayRate::createRate($rateInfo);
        if($result > 0){
            $this->setting_log($this->user->id,self::SETTING_TABLE,$result,SettingLog::LOG_INSERT,$rateInfo);
            return stored('');
        }else if($result == -1){
            return failed('资方不存在');
        }else if($result == -2){
            return failed('期数不在首页设置期数范围内');
        }else if($result == -3){
            return failed('当前资方的期数已经存在');
        }else{
            return failed('创建失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(SettingRepayRateRequest $request, $id)
    {
        $rateInfo = $request->only(['channel_code','period','lending_rate','insurance_rate','insure_rate','month_insurance_rate','manage_rate','repayment_rate','fines_rate']);
        $result = SettingRepayRate::updateRate($rateInfo,$id);
        if($result > 0){
            $this->setting_log($this->user->id,self::SETTING_TABLE,$id,SettingLog::LOG_UPDATE,$rateInfo);
            return updated('');
        }else if($result == -1){
            return failed('资方不存在');
        }else if($result == -2){
            return failed('期数不在首页设置期数范围内');
        }else if($result == -3){
            return failed('当前资方的期数已经存在');
        }else{
            return failed('修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = SettingRepayRate::destroy($id);
        if($res){
            $this->setting_log($this->user->id,self::SETTING_TABLE,$id,SettingLog::LOG_DELETE);
            return deleted();
        }else{
            return failed('删除失败');
        }
    }
}
